package com.pocket.demo.base.utils;

import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Arrays;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * 获取当前请求 Locale 【demo-i18n-2.1】
 * 请求的 Locale 通过 LocaleResolver 处理，默认：AcceptHeaderLocaleResolver。
 *    通过 WebMvcAutoConfiguration 注入，从 Accept-Language 请求头中获取 Locale 信息。
 **/
public class LocaleContextUtil {

    public static final Pattern IMAGE_CONTENT_TYPE_PATTERN = Pattern.compile("image/([^;]+);*");

    /**
     * 返回当前地区，非 CHINA 默认 US
     * @return
     */
    public static Locale getLocale() {
        Locale locale = LocaleContextHolder.getLocale();
        if (!Arrays.asList(Locale.CHINA, Locale.TAIWAN, Locale.US).contains(locale)) {
            locale = locale.getLanguage().equals(Locale.CHINA.getLanguage()) ? Locale.CHINA : Locale.US;
        }
        return locale;
    }

}
